/*******************************************************************************
 * Copyright (C) 2018-2020 CAROTA Technology Crop. <www.carota.ai>.
 * All Rights Reserved.
 *
 * Unauthorized using, copying, distributing and modifying of this file,
 * via any medium is strictly prohibited.
 *
 * Proprietary and confidential.
 ******************************************************************************/

package com.sun.dailyprj.pki;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FormatUtil {
    /**
     * format seconds to hh:mm:ss
     * @param l
     * @return
     */
    public static String formatTime(long l) {
        l = l/1000;
        long hh = l / 3600;
        long mm = (l % 3600) / 60;
        long ss = (l % 60);
        if(hh >0) return String.format("%02d:%02d", mm, ss);
        else if(mm >0) return String.format("%02d:%02d" , mm, ss);
        else if(ss>0) return String.format("%02d:%02d",00,ss);
        else return "00:00";
    }

    /**
     * 利用正则表达式去除字符串中的空格,tab，换行
     * @param src
     * @return
     */
    public static String replaceBlank(String src) {
        String dest = "";
        if (src != null) {
            Pattern pattern = Pattern.compile("\t|\r|\n|\\s*");
            Matcher matcher = pattern.matcher(src);
            dest = matcher.replaceAll("");
        }
        return dest;
    }

    public static String formatDate(long timestamp) {
        Date date = new Date(timestamp);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public static String formatDate(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public static String formatTime(long timestamp, String form) {
        Date date = new Date(timestamp);
        SimpleDateFormat format = new SimpleDateFormat(form);
        return format.format(date);
    }
}
